package cn.edu.scau.cmi.linhuanrui.client;

import cn.edu.scau.cmi.linhuanrui.singleton.Marshal;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class MultitonClient extends Application {
	@Override
	public void start(Stage primaryStage) {
		primaryStage.setTitle("Singleton");
		primaryStage.setResizable(false);

		Label label = new Label("多例模式\n\n创建元帅名字：");
		label.setTranslateX(20);
		label.setTranslateY(10);
		TextField text = new TextField();
		text.setTranslateY(25);
		text.setTranslateX(20);
		Button ok = new Button("确定");
		ok.setTranslateY(25);
		ok.setTranslateX(25);
		TextArea show = new TextArea();
		show.setTranslateX(20);
		show.setTranslateY(25);
		show.setMinHeight(250);
		show.setMinWidth(300);
		ok.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				int num = Marshal.getNum();
				Marshal marshal = Marshal.getInstance(text.getText());
				if (marshal == null)
					show.appendText("你已经创建了十个元帅了，不能再创建了\n");
				else if (num < Marshal.getNum())
					show.appendText("已成功创建第" + Marshal.getNum() + "个元帅的对象：\n" + marshal.getName() + marshal + "\n");
				else
					show.appendText("该元帅对象已被创建，不能再创建了！你获得的对象为：\n" + marshal.getName() + marshal + "\n");
			}

		});
		FlowPane root = new FlowPane();
		root.getChildren().add(label);
		root.getChildren().add(text);
		root.getChildren().add(ok);
		root.getChildren().add(show);
		primaryStage.setScene(new Scene(root, 550, 350));
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
